package org.example.controller;


import org.example.entity.Absent;
import org.example.form.SearchForm;
import org.example.service.AbsentService;
import org.example.service.BuildingService;
import org.example.service.DormitoryService;
import org.example.service.StudentService;
import org.example.vo.PageVO;
import org.example.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@RestController ////RestControler =  Controller + ResponseBody 该类交给ioc容器管理，并返回json数据。. 所有的方法默认都会返回 HTTP 响应体中的内容，而不是返回视图。

@RequestMapping("/absent")
public class AbsentController {
  @Autowired
  private AbsentService absentService;
  @Autowired
  private BuildingService buildingService;
  @Autowired
  private DormitoryService dormitoryService;
  @Autowired
  private StudentService studentService;
 @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable("page") int page, @PathVariable("size") int size) {
     PageVO pageVO = this.absentService.list(page, size);
     return ResultVO.success(pageVO);
 }
 @GetMapping("/search")
    public ResultVO search(SearchForm searchForm) {
     return ResultVO.success(absentService.search(searchForm));

 }
 @GetMapping("/buildingList")
    public ResultVO buildingList() {
     return ResultVO.success(buildingService.list());
 }
 @GetMapping("/findDormitoryByBuildingId/{buildingId}")
    public ResultVO findDormitoryByBuildingId(@PathVariable("buildingId") int buildingId) {
     return ResultVO.success(dormitoryService.findDormitoryByBuildingId(buildingId));
    }
    @GetMapping("/findStudentByDormitoryId/{dormitoryId}")
    public ResultVO findStudentByDormitoryId(@PathVariable("dormitoryId") int dormitoryId) {
        return ResultVO.success(studentService.findStudentByDormitoryId(dormitoryId));
    }
    @PostMapping("/save")
    public ResultVO save(@RequestBody Absent absent) {
     String createDate = absent.getCreateDate();
     absent.setCreateDate(createDate.substring(0, 10));
        boolean save = absentService.save(absent);
        if (save) {
            return ResultVO.success(null);
        } else {
            return ResultVO.error("error");
        }
    }
}

